import dialogHtml from 'ngtemplate!html!../../../tpls/dialog/success.dialog.html';

class ServicePublishController {
    constructor($rootScope, $scope, $cookieStore, $timeout, $state, ngDialog, dialogService, ServiceService, iRunner, getRaceDetail, RaceService){

        var vm = this;

        vm.$rootScope     = $rootScope;
        vm.$cookieStore   = $cookieStore;
        vm.$timeout       = $timeout;
        vm.$state         = $state;
        vm.ngDialog       = ngDialog;
        vm.dialogService  = dialogService;
        vm.ServiceService = ServiceService;
        vm.RaceService = RaceService
        vm.iRunner        = iRunner;
        vm.getRaceDetail = getRaceDetail;

        // 详细设置
        var Detail = vm.getRaceDetail.data;
        //显示赛事详细设置
        // 比赛实时直播服务
        vm.live = Detail.live_show == 'yes' ? true : false;
        vm.live_show = vm.live;
        // 成绩短信通知服务
        vm.achievementsms = Detail.sms == 'yes' ? true : false;
        vm.achievementsms_show = vm.achievementsms;
        // 手机成绩查询服务
        vm.achievementselect = Detail.search_result_show == 'yes' ? true : false;
        vm.achievementselect_show = vm.achievementselect;
        // 选手自助芯片绑定服务
        vm.chipbind = Detail.chip_bind == 'yes' ? true : false;
        vm.chipbind_show = vm.chipbind;
        // 选手参赛信息查询服务
        vm.runnerinfoselect = Detail.reg_query == 'yes' ? true : false;
        vm.runnerinfoselect_show = vm.runnerinfoselect;
        // 电子证书下载服务
        vm.certificate = Detail.cert_download == 'yes' ? true : false;
        vm.certificate_show = vm.certificate;
        // 报名服务
        // vm.register = Detail.register == 'yes' ? true : false;
        vm.register = true;  //报名服务默认显示
        vm.register_show = vm.register;
        //自助选号服务
        vm.picknum = Detail.picknum == 'yes' ? true : false;
        // 视频直播功能
        vm.video_live = Detail.video_live == 'yes' ? true : false;
        vm.video_live_show = vm.video_live;
        // 选手个人视频功能
        vm.personal_video = Detail.personal_video == 'yes' ? true : false;
        vm.personal_video_show = vm.personal_video;
        // 比赛相册
        vm.personal_photo = Detail.race_album == 'yes' ? true : false;
        vm.personal_photo_show = vm.personal_photo;
        // 成绩列表
        vm.scorelist = true;
        vm.scorelist_show = vm.scorelist;

        //比赛实时直播服务
        vm.liveUrl = 'http://' + location.host + '/' + vm.getRaceDetail.data.short_name + '/live/index';
        $('#qrcode-live').qrcode({
            width: 100,
            height: 100,
            text: vm.liveUrl
        });

        //手机成绩查询服务
        vm.achievementselectUrl = 'http://' + location.host + '/' + $rootScope.raceinfo.short_name + '/score';
        $('#qrcode-achievementselect').qrcode({
            width: 100,
            height: 100,
            text: vm.achievementselectUrl
        });

        //选手自助芯片绑定服务
        vm.chipbindUrl = 'http://' + location.host + '/' + $rootScope.raceinfo.short_name + '/chip';
        $('#qrcode-chipbind').qrcode({
            width: 100,
            height: 100,
            text: vm.chipbindUrl
        });

        //选手参赛信息查询服务
        vm.runnerinfoselectUrl = 'http://' + location.host + '/' + $rootScope.raceinfo.short_name + '/regquery'; 
        $('#qrcode-runnerinfoselect').qrcode({
            width: 100,
            height: 100,
            text: vm.runnerinfoselectUrl
        });

        //报名手机端链接
        vm.registerUrl = 'http://' + location.host + '/' + $rootScope.raceinfo.short_name + '/register/disclaimer'; 
        $('#qrcode-register').qrcode({
            width: 100,
            height: 100,
            text: vm.registerUrl
        });

        // 视频直播手机端链接
        vm.videoLiveUrl = 'http://' + location.host + '/' + $rootScope.raceinfo.short_name + '/video/live'; 
        $('#qrcode-video-live').qrcode({
            width: 100,
            height: 100,
            text: vm.videoLiveUrl
        });

        // 个人视频手机端链接
        vm.personalVideoUrl = 'http://' + location.host + '/' + $rootScope.raceinfo.short_name + '/video'; 
        $('#qrcode-personal-video').qrcode({
            width: 100,
            height: 100,
            text: vm.personalVideoUrl
        });

        // 成绩列表链接
        vm.scorelistUrl = 'http://' + location.host + '/' + $rootScope.raceinfo.short_name + '/score/courses'; 
        $('#qrcode-scorelist').qrcode({
            width: 100,
            height: 100,
            text: vm.scorelistUrl
        });

        // 保存设置详情
        vm.save = function() {
            // 拼接POST数据格式
            var params = {
                // 比赛实时直播服务
                live: vm.live ? 'yes' : 'no',
                // 成绩短信通知服务
                // sms: vm.achievementsms ? 'yes' : 'no',
                sms: 'yes',
                // 手机成绩查询服务
                search_result: vm.achievementselect ? 'yes' : 'no',
                // 选手自助芯片绑定服务
                chip_bind: vm.chipbind ? 'yes' : 'no',
                // 选手参赛信息查询服务
                reg_query: vm.runnerinfoselect ? 'yes' : 'no',
                // 电子证书下载服务
                cert_download: vm.certificate? 'yes' : 'no',
                // 报名服务
                // register: vm.register? 'yes' : 'no',
                register: 'yes',
                // 自助选号服务
                picknum: vm.picknum ? 'yes' : 'no',
                // 视频直播
                video_live: vm.video_live ? 'yes' : 'no',
                // 个人视频
                personal_video: vm.personal_video ? 'yes' : 'no',
                // 赛事相册
                race_album: vm.personal_photo ? 'yes' : 'no',

            }

            // 更新赛事
            vm.RaceService.postRaceUpdate(params, vm.$cookieStore.get('access_token'), vm.$state.params.race_id)
            .then(function(data) {
                // 成功调用
                if (data.code==0) {
                    // 模态框提示文本
                    $scope.text = '更新成功';
                    // 提示用户成功调用
                    vm.ngDialog.open({
                        template: dialogHtml,
                        className: 'ngdialog-theme-default ngdialog-alert ngdialog-success',
                        scope: $scope
                    });
                    // 延迟1.5秒后, 刷新页面
                    vm.$timeout(function() {
                        vm.$state.reload();
                        vm.ngDialog.close();
                    }, 1500)
                }
                // 调用失败, 提示用户
                (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);
                // 登录信息已过期, 请重新登录
                data.code==-1001&&vm.dialogService.error(data.message, true);
            }, function(data) {
                console.log('error')
            });

        }
    }
}

ServicePublishController.$inject = ['$rootScope', '$scope', '$cookieStore', '$timeout', '$state', 'ngDialog', 'dialogService', 'ServiceService', 'iRunner', 'getRaceDetail', 'RaceService']; 

export default ServicePublishController;